package com.bjyhl.medical.main.news.list;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;

import com.bjyhl.core.fragments.LatteFragment;
import com.bjyhl.medical.R;
import com.bjyhl.medical.main.EcBottomFragment;
import com.bjyhl.medical.main.news.NewsFragment;
import com.bjyhl.medical.main.news.detail.NewsDetailFragment;
import com.bjyhl.ui.recycler.MultipleFields;
import com.bjyhl.ui.recycler.MultipleItemEntity;
import com.bjyhl.ui.refresh.RefreshHandler;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;

import java.util.List;

/**
 * Created by Natalya on 2019/1/27 18:22
 * Note: 主页fragment
 */

public class NewsListFragment extends LatteFragment {

    private static final String TAG = "NewsListFragment";
    public static final String ARG_NEWSCAT_ID = "NEWSCAT_ID";
    private int mNewsCatId = -1;
    private List<MultipleItemEntity> mData = null;

    private RecyclerView mRecyclerView = null;
    private SwipeRefreshLayout mSwipeRefreshLayout = null;
    private RefreshHandler mRefreshHandler = null;

    public static NewsListFragment newInstance(int contentId) {
        final Bundle args = new Bundle();
        args.putInt(ARG_NEWSCAT_ID, contentId);
        final NewsListFragment fragment = new NewsListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_news_content_list;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        mRecyclerView = $(R.id.rv_news_cnt_list);
        mSwipeRefreshLayout = $(R.id.srl_news_list);

        mRefreshHandler = RefreshHandler.create(mSwipeRefreshLayout, mRecyclerView, new NewsDataConverter(),
                "http://192.168.8.101:3000/v1/news/getNewsList/" + mNewsCatId + "/");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mNewsCatId = args.getInt(ARG_NEWSCAT_ID);
        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initRecyclerView();
        initRefreshLayout();
        mRefreshHandler.firstPage();
    }

    private void initRecyclerView() {
        final LinearLayoutManager manager =
                new LinearLayoutManager(getContext(), StaggeredGridLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(manager);

        // 父级窗体
        final NewsFragment newsFragment = getParentFragments();
        final EcBottomFragment ecBottomFragment = newsFragment.getParentFragments();
        mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                // 打开新闻详情页面
                MultipleItemEntity entity = (MultipleItemEntity) (adapter.getData().get(position));
                int newsId = entity.getField(MultipleFields.ID);
                NewsDetailFragment newsDetailFragment = NewsDetailFragment.create(newsId);
                ecBottomFragment.getSupportDelegate().start(newsDetailFragment);
            }
            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {}
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {}
            @Override
            public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {}
        });
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        mSwipeRefreshLayout.setProgressViewOffset(true, 120, 260);
    }


}
